//package com.example.springcloudwinter.service;
//
//
//import com.example.springcloudwinter.dto.ResultEntity;
//import com.example.springcloudwinter.mapper.VIPMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.sql.SQLException;
//
//@Service
//public class VIPService {
//
//	@Autowired
//	private VIPMapper vipMapper;
//
//	public ResultEntity<?> openMembershipByUserId (String userId) {
//		ResultEntity<Integer> result=new ResultEntity<>();
//		try {
//			int i = vipMapper.openMembershipByUserId(userId);
//			result.setDetail(i).setCode(200);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return result;
//	}
//
//	public ResultEntity<?> checkVIPByUserId  (String userId) {
//		ResultEntity<Boolean> result=new ResultEntity<>();
//		result.setDetail(false);
//
//		try {
//			int isVip = vipMapper.checkVIPByUserId(userId);
//			if(isVip==0){
//				result.setDetail(false);
//			} else{
//				result.setDetail(true);
//			}
//			result.setCode(200);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return result;
//	}
//}
package com.example.springcloudwinter.service;

import com.example.springcloudwinter.dto.ResultEntity;
import com.example.springcloudwinter.mapper.VIPMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Service
public class VIPService {

	private static final Logger logger = LoggerFactory.getLogger(VIPService.class);

	@Autowired
	private VIPMapper vipMapper;

	public ResultEntity<Object> openMembershipByUserId(String userId) {
		if (userId == null || userId.isEmpty()) {
			logger.error("Invalid userId: {}", userId);
			return new ResultEntity<>();
		}

		try {
			int rows = vipMapper.openMembershipByUserId(userId);
			return new ResultEntity<>().setSuccess(rows);
		} catch (SQLException e) {
			logger.error("Error opening membership for userId: {}", userId, e);
			return new ResultEntity<>();
		}
	}

	public ResultEntity<Object> checkVIPByUserId(String userId) {
		if (userId == null || userId.isEmpty()) {
			logger.error("Invalid userId: {}", userId);
			return new ResultEntity<>();
		}

		try {
			int count = vipMapper.checkVIPByUserId(userId);
			boolean isVip=false;
			if(count>0){
				isVip=true;
			}
			return new ResultEntity<>().setSuccess(isVip);
		} catch (SQLException e) {
			logger.error("Error checking VIP status for userId: {}", userId, e);
			return new ResultEntity<>();
		}
	}
}